const swapPoll = async(browser, page) => {
    try {
        await page.goto("https://testnet.zenithfinance.xyz/pool");
        const Connect = await page.locator("xpath=//button[text()='Connect']").all();
        if (Connect.length > 0){
            await page.waitForTimeout(2000);
            // 操作新页面中的 'MetaMask
            const [xhlLoginpage] = await Promise.all([
                browser.waitForEvent('page', {timeout:240000}), // 等待新窗口打开
                page.locator("xpath=//div[text()='MetaMask']").click(),
                console.log('点击钱包')
            ]);
            // 等待新页面加载
            await xhlLoginpage.waitForLoadState('networkidle', {timeout:240000});
            if (xhlLoginpage){
                console.log('新页面加载完成')
                await xhlLoginpage.locator('xpath=//button[@data-testid="confirm-btn"]').click();
                console.log("连接成功")
            }
        } else {
            console.log('已经连接钱包')
        }
        await page.locator("xpath=//a[@id='join-pool-button']").click();
        await page.locator('xpath=//div[2]/div[@id="add-liquidity-input-tokenb"]//button').click();
        await page.locator("xpath=//div[@data-testid='common-base-USDC']/div[text()='USDC']").click();
        await page.locator("xpath=//div[contains(@class, 'FeeSelector__Select')]/button[1]").click();
        await page.locator("xpath=//div[@id='add-liquidity-input-tokena']//input").fill("0.01");
        const CurrentPrice = await page.locator("xpath=//div[text()='Current price:']/following-sibling::*[1]").textContent()
        await page.locator("xpath=//div[text()='Low price']/following-sibling::*[1]").fill(CurrentPrice);
        await page.locator("xpath=//div[text()='High price']/following-sibling::*[1]").fill(CurrentPrice);
        await page.locator("xpath=//div[@id='Preview']").click(); 

    } catch (err) {
        console.error(err);
    }
};

module.exports = { swapPoll };